#!/bin/sh
# PCP QA Test No. 833
#
# Memory leak associated with PDU buffers remaining pinned after calling
# pmDestroyContext().
# See http://oss.sgi.com/bugzilla/show_bug.cgi?id=1057
#
# Memory corruption after duplicating contexts with attributes after calling
# pmDestroyContext().
# See http://oss.sgi.com/bugzilla/show_bug.cgi?id=1129
# 
# Copyright (c) 2014 Ken McDonell.  All Rights Reserved.
# Copyright (c) 2016 Red Hat.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

_filter_pdu()
{
    # free pdubuf[size]: 0xe61000[28672] 0xe5c000[17408]
    sed \
	-e '/free pdubuf/s/0x[0-9a-f]*\[[0-9]*\]/addr[size]/g' \
    | $PCP_AWK_PROG '
/free pdubuf/	{ if (NF <= 9) {
		    print "   free pdubuf[size]: ... <= 7 buffers ..."
		    next
		  }
		}
		{ print }'
}

_filter_date()
{
    sed \
	-e 's/[A-Z][a-z][a-z] [A-Z][a-z][a-z]  *[0-9][0-9]* [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9]$/DATE/' \
    # end
}

_filter_pmid()
{
    sed \
	-e 's/^29\./xxx29./' \
	-e 's/^[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*:/PMID:/' \
	-e 's/^xxx//' \
	-e '/kernel.all.load/s/([0-9][0-9.]*)/(PMID)/' \
	-e '/hinv.ncpu/s/([0-9][0-9.]*)/(PMID)/' \
    # end
}

status=1	# failure is the default!
trap "cd $here; rm -rf $tmp.*; exit \$status" 0 1 2 3 15

# real QA test starts here
echo "=== pmNewContext ==="
src/churnctx -z -s 400 -a archives/bug1057 kernel.all.load hinv.ncpu disk.dev.total 2>&1 | _filter_pdu

echo
echo "=== pmDupContext ==="
src/churnctx -d -z -s 400 -a archives/bug1057 kernel.all.load hinv.ncpu disk.dev.total 2>&1 | _filter_pdu

echo
echo "=== host context, and pmNewContext ... just to be sure ==="
src/churnctx -s 10 -t 0.2sec kernel.all.load hinv.ncpu sample.byte_ctr 2>&1 \
| _filter_pdu \
| _filter_pmid \
| _filter_date

echo
echo "=== local: context with attributes, and pmDupContext ==="
src/churnctx -d -s 10 -t 0.2sec -h local: kernel.all.load hinv.ncpu sample.byte_ctr 2>&1 \
| _filter_pdu \
| _filter_pmid \
| _filter_date

# success, all done
status=0

exit
